#include "weatherwindow.h"
#include "ui_weatherwindow.h"
#include <QDateTime>
#include "weatherConfigure.h"
#include <QJsonArray>


weatherWindow::weatherWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::weatherWindow)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    connect(this->timer,&QTimer::timeout,this,&weatherWindow::setTime);
    timer->start(1000);
    manager = new QNetworkAccessManager();
    this->vectorDate = new QVector<QLabel*>();
    this->vectorIcon = new QVector<QLabel*>();
    this->vectorWeather = new QVector<QLabel*>();
    vectorDate->append(ui->label_8);
    vectorDate->append(ui->label_9);
    vectorDate->append(ui->label_10);
    vectorDate->append(ui->label_11);
    vectorDate->append(ui->label_12);
    vectorDate->append(ui->label_13);
    vectorDate->append(ui->label_14);

    vectorIcon->append(ui->label);
    vectorIcon->append(ui->label_2);
    vectorIcon->append(ui->label_6);
    vectorIcon->append(ui->label_3);
    vectorIcon->append(ui->label_4);
    vectorIcon->append(ui->label_5);
    vectorIcon->append(ui->label_7);

    vectorWeather->append(ui->label_15);
    vectorWeather->append(ui->label_16);
    vectorWeather->append(ui->label_17);
    vectorWeather->append(ui->label_18);
    vectorWeather->append(ui->label_21);
    vectorWeather->append(ui->label_19);
    vectorWeather->append(ui->label_20);
    getWeather();

}

weatherWindow::~weatherWindow()
{
    delete ui;
}

void weatherWindow::on_pushButton_clicked()
{
    this->parentWidget()->show();
    this->hide();
}

void weatherWindow::setTime()
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QString newtime = currentTime.toString("yyyy年MM月dd日\nhh时mm分ss秒 dddd");
    ui->label_24->setText(newtime);
}

void weatherWindow::getWeather()
{
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QString url1 = "http://gfeljm.tianqiapi.com/api?unescape=1&version=%1&appid=%2&appsecret=%3&ext=&cityid=";
    QString url2 = url1.arg(weatherVersion).arg(weatherAppid).arg(weatherAppsecret);
    QUrl url(url2);
    QNetworkRequest request(url);
    QNetworkReply *reply = manager->get(request);
    // 等待请求完成（同步方式）
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
    // 检查请求是否成功
    if (reply->error() == QNetworkReply::NoError) {
        // 读取响应数据
        QByteArray responseData = reply->readAll();


        // 解析JSON数据
        QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);

        if (jsonDoc.isNull()) {
            qDebug() << "Failed to parse JSON!";
            return;
        }

        else if(jsonDoc.isObject()){
            QJsonObject object = jsonDoc.object();
            QString city = object.value("city").toString();
            ui->pushButton_2->setText(city);
            QJsonArray data = object.value("data").toArray();

            for (int i = 0;i < data.size();i++) {
                QJsonObject obj = data[i].toObject();
                QString date = obj.value("day").toString();
                QString max = obj.value("tem1").toString();
                QString min = obj.value("tem2").toString();
                QString date1 = date.split("（").at(0);
                QString week =obj.value("week").toString();
                QString dateValue = date1+"\n"+week+"\n"+min+"℃~"+max+"℃";
                vectorDate->at(i)->setText(dateValue);

                QString icon = obj.value("wea_img").toString();
                QString iconStr = QString("border-image: url(:/images/weather/%1.png);").arg(icon);
                vectorIcon->at(i)->setStyleSheet(iconStr);

                vectorWeather->at(i)->setText(obj.value("wea").toString());
                if(i==0){
                    ui->label_23->setStyleSheet(iconStr);
                    ui->label_22->setText(obj.value("wea").toString());
                }
            }
        }

}
}

void weatherWindow::on_pushButton_2_clicked()
{
    qDebug()<<"刷新";
    QString url1 = "http://gfeljm.tianqiapi.com/api?unescape=1&version=%1&appid=%2&appsecret=%3&ext=&cityid=";
    QString url2 = url1.arg(weatherVersion).arg(weatherAppid).arg(weatherAppsecret);
    QUrl url(url2);
    QNetworkRequest request(url);
    QNetworkReply *reply = manager->get(request);
    // 等待请求完成（同步方式）
    QEventLoop loop;
    QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
    loop.exec();
    // 检查请求是否成功
    if (reply->error() == QNetworkReply::NoError) {
        // 读取响应数据
        QByteArray responseData = reply->readAll();


        // 解析JSON数据
        QJsonDocument jsonDoc = QJsonDocument::fromJson(responseData);

        if (jsonDoc.isNull()) {
            qDebug() << "Failed to parse JSON!";
            return;
        }

        else if(jsonDoc.isObject()){
            QJsonObject object = jsonDoc.object();
            QString city = object.value("city").toString();
            ui->pushButton_2->setText(city);
            QJsonArray data = object.value("data").toArray();

            for (int i = 0;i < data.size();i++) {
                QJsonObject obj = data[i].toObject();
                QString date = obj.value("day").toString();
                QString max = obj.value("tem1").toString();
                QString min = obj.value("tem2").toString();
                QString date1 = date.split("（").at(0);
                QString week =obj.value("week").toString();
                QString dateValue = date1+"\n"+week+"\n"+min+"℃~"+max+"℃";
                vectorDate->at(i)->setText(dateValue);

                QString icon = obj.value("wea_img").toString();
                QString iconStr = QString("border-image: url(:/images/weather/%1.png);").arg(icon);
                vectorIcon->at(i)->setStyleSheet(iconStr);

                vectorWeather->at(i)->setText(obj.value("wea").toString());
                if(i==0){
                    ui->label_23->setStyleSheet(iconStr);
                    ui->label_22->setText(obj.value("wea").toString());
                }
            }
        }

}
}
